<?php
/**
	 * format_calculation
	 * Returns the supplied number with a submitted number of decimal points and no thousand separator, so the value can be used for calculations.
	 */
  function format_calculation($number = 0, $decimals = 2, $sanitize = FALSE)
	{
		if ($sanitize)
		{
			// Remove any non numeric or decimal point characters.
			$number = trim(preg_replace('/([^0-9\.])/i','',$number));
		}
	
		return round($number, $decimals);
	}

	/** 
	 * format_value
	 * Returns a value formatted to a specified number of decimals using the set decimal and thousand separators.
	 */
	   function format_value($type = 'currency', $value = FALSE, $format = TRUE, $decimals = 2, $internal_format = FALSE, $currency_name = FALSE, $weight_type = FALSE) 
	{
		// Set decimals and remove any characters that are not number or decimal point (i.e. thousand comma separators).
		$decimals = (! $format && $decimals === FALSE) ? 4 : $decimals;
		$value = $this->format_calculation($value, $decimals);
		
		if ($format)
		{
			// Get data source for formatting.
			if ($currency_name && ! $internal_format && $this->get_enabled_status('currency'))
			{
				$sql_where = array(
					$this->flexi->cart_database['currency']['columns']['name'] => $currency_name,
					$this->flexi->cart_database['currency']['columns']['status'] => 1
				);
				
				$currency_data = $this->get_currency_data(FALSE, $sql_where, TRUE);
			}
			else
			{
				$path_alias = ($internal_format) ? 
					$this->flexi->cart_contents['settings']['currency']['default'] : $this->flexi->cart_contents['settings']['currency'];			
			}

			// Set separators.
			$decimal_separator = (! empty($currency_data)) ? 
				$currency_data[$this->flexi->cart_database['currency']['columns']['decimal_separator']] : $path_alias['decimal_separator'];
			$thousand_separator = (! empty($currency_data)) ? 
				$currency_data[$this->flexi->cart_database['currency']['columns']['thousand_separator']] : $path_alias['thousand_separator'];
			
			// Apply the current currencies decimal and thousand separator
			$value = number_format($value, $decimals, $decimal_separator, $thousand_separator);
				
			// If a currency, set symbol and return value.
			if ($type == 'currency')
			{
				$currency_symbol = (! empty($currency_data)) ? 
					$currency_data[$this->flexi->cart_database['currency']['columns']['symbol']] : $path_alias['symbol'];
				$currency_symbol_suffix = (! empty($currency_data)) ? 
					$currency_data[$this->flexi->cart_database['currency']['columns']['symbol_suffix']] : $path_alias['symbol_suffix'];
					
				$value = (! $currency_symbol_suffix) ? $currency_symbol.$value : $value.$currency_symbol;
			}
			// Else, if a weight, set symbol and return value.
			else if ($type == 'weight')
			{
				// Get the weight type symbol.
				$weight_type = (! $weight_type) ? $this->flexi->cart_contents['settings']['configuration']['weight_type'] : $weight_type;
				$weight_data = $this->get_weight_types($weight_type);
				$weight_symbol = (isset($weight_data['symbol'])) ? $weight_data['symbol'] : FALSE;

				$value .= $weight_symbol;
			}
			return $value;
		}
		else
		{
			return ($type == 'weight') ? round($value, $decimals) : number_format($value, $decimals, '.', FALSE);
		}
	}	
	
	/**
	 * calculate_string
	 * Calculate string as an equation, without using eval()
	 */
	   function calculate_string($string) 
	{
		$string = trim($string);
		$string = preg_replace('[^0-9\+\-\*\/\(\) ]', '', $string); // Remove non-numbers chars, except for math operators

		$calculate = create_function('', 'return ('.$string.');');
		
		return (float)$calculate();
	}
	
	/**
	 * is_positive
	 * Validates if a value is a number and is positive (> 0).
	 */
	   function is_positive($value) 
	{
		return (isset($value) && is_numeric($value) && ($value > 0));
	}
	
	/**
	 * non_negative
	 * Validates if a value is a number and is not negative (0 or higher).
	 */
	   function non_negative($value) 
	{
		return (isset($value) && is_numeric($value) && ($value >= 0));
	}

	/**
	 * get_weight_types
	 * Returns either an array of data for a specific weight type or a multi-dimensional array all weight types and their data.
	 */
	   function get_weight_types($name = FALSE)
	{
		$weight_types = array(
			'gram' => array('name' => 'Gram', 'conversion' => 1, 'symbol' => 'g'),
			'kilogram' => array('name' => 'Kilogram', 'conversion' => 1000, 'symbol' => 'kg'),
			'avoir_ounce' => array('name' => 'Avoir Ounce', 'conversion' => 28.3495, 'symbol' => 'oz'),
			'avoir_pound' => array('name' => 'Avoir Pound', 'conversion' => 453.5924, 'symbol' => 'lb'),
			'troy_ounce' => array('name' => 'Troy Ounce', 'conversion' => 31.1035, 'symbol' => 't oz'),
			'troy_pound' => array('name' => 'Troy Pound', 'conversion' => 373.2417, 'symbol' => 't lb'),
			'carat' => array('name' => 'Carat', 'conversion' => 0.2, 'symbol' => 'ct')
		);
		
		return (isset($weight_types[$name])) ? $weight_types[$name] : $weight_types;
	}
	
	/**
	 * database_date_time
	 * Format the current or submitted date and time (in seconds). 
	 * Additional time can be added / subtracted.
	 */
	   function database_date_time($apply_time = 0, $date_time = FALSE, $force_unix = FALSE)
	{
		// Get cart config settings.
		$cart_settings = $this->config->item('settings', 'flexi_cart');
	
		// Get timestamp of either submitted date or current date.
		if ($date_time)
		{
			$date_time = (is_numeric($date_time) && strlen($date_time) == 10) ? $date_time : strtotime($date_time);
		}
		else
		{
			$date_time = time();
		}
		
		// Add or subtract any submitted apply time.
		$date_time += $apply_time;
	
		// If database date/time is set as UNIX via config file, or if a unix timestamp has been requested.
		if ((is_numeric($cart_settings['date_time']) && strlen($cart_settings['date_time']) == 10) || $force_unix)
		{
			return $date_time; 
		}
		else if (is_string($cart_settings['date_time']) && strtotime($cart_settings['date_time'])) // MySQL datetime.
		{
			return date('Y-m-d H:i:s', $date_time);
		}
		else // Return date/time set via config file.
		{
			return $cart_settings['date_time'];
		}
	}
}
?>